# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def IsBalanced_Solution(self, pRoot):
        # write code here
        # 左右两个子树的高度差不能大于1，且左右两个子树也为平衡二叉树
        if not pRoot:
            return True
        if self.helper(pRoot) > 0:
            return True
        else:
            return False
    def helper(self, root):
        if not root:
            return 0
        left = self.helper(root.left)
        right = self.helper(root.right)
        if abs(left - right) > 1:
            return float('-inf')
        return max(left, right) + 1


class Solution:
    def IsBalanced_Solution(self, pRoot):
        return self.helper(pRoot) != 1
    def helper(self, root):
        '''
        如果有-1，那么一路返回-1
        :param root:
        :return:
        '''
        if not root:
            return 0
        left = self.helper(root.left)
        if left == -1:
            return -1
        right = self.helper(root.right)
        if right == -1:
            return -1
        return -1 if abs(left - right) > 1 else max(left, right) + 1